/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

import java.io.Serializable;

/**
 *
 * @author minaev
 */
public class CoefConfigurator implements Serializable {

    private Configurator conf = Configurator.getConf();
    
    private static CoefConfigurator coef_conf = null;

    public CoefConfigurator() {
    }

    private String getNameCoef0Sensor(int nsensor) {
        if (nsensor >= 0 && nsensor < Configurator.COUNT_SENSOR) {
            return "coef0_s" + nsensor;
        } else {
            StendLogger.log(StendLogger.MSG_WARNING, "Not exists (bad nsensor:"
                    + nsensor + ") "
                    + getNameCoef0Sensor(nsensor) + ". Loaded default "
                    + getNameCoef0Sensor(nsensor) + "=0");
            return null;
        }
    }

    private String getNameCoefPropSensor(int nsensor) {
        if (nsensor >= 0 && nsensor < Configurator.COUNT_SENSOR) {
            return "coefprop_s" + nsensor;
        } else {
            StendLogger.log(StendLogger.MSG_WARNING, "Not exists (bad nsensor:"
                    + nsensor + ") "
                    + getNameCoef0Sensor(nsensor) + ". Loaded default "
                    + getNameCoef0Sensor(nsensor) + "=0");
            return null;
        }
    }
    
    private String getNameCoef0Position(int nmotor) {
        return "coef0_pos_" + nmotor;
    }

    private String getNameCoefPropPosition(int nmotor) {
        return "coefprop_pos_" + nmotor  ;
    }
    
    private String getNameMaxPos(int nmotor){
        return "max_pos_"+nmotor;
    }
    
    private String getNameMinPos(int nmotor){
        return "min_pos_"+nmotor;
    }
    
    
    private long getMaxMinPos(String prop_name) {
        String buf = conf.getProperty(prop_name);
        if (buf != null) {
            return Long.valueOf(buf);
        } else {
            StendLogger.log(StendLogger.MSG_WARNING, "Not exists "
                    + prop_name + ". Loaded default " + prop_name + "=0");
            conf.addProperty(prop_name, "0");
            return 0;
        }
    }
    

    private int getCoef0(String prop_name) {

        String buf = conf.getProperty(prop_name);
        if (buf != null) {
            return Integer.valueOf(buf);
        } else {
            StendLogger.log(StendLogger.MSG_WARNING, "Not exists "
                    + prop_name + ". Loaded default " + prop_name + "=0");
            conf.addProperty(prop_name, "0");
            return 0;
        }

    }

    private double getCoefProp(String prop_name) {
        String buf = conf.getProperty(prop_name);
        if (buf != null) {
            return Double.valueOf(buf);
        } else {
            StendLogger.log(StendLogger.MSG_WARNING, "Not exists "
                    + prop_name + ". Loaded default " + prop_name + "=1");
            conf.addProperty(prop_name, "1");
            return 0;
        }
    }

    public int getSensorCoef0(int nsensor) {
        return getCoef0(getNameCoef0Sensor(nsensor));
    }

    public double getSensorCoefProp(int nsensor) {
        return getCoefProp(getNameCoefPropSensor(nsensor));
    }
    
    public void setSensorCoef0(int nsensor, int coef0){
        conf.setProperty(getNameCoef0Sensor(nsensor), ""+coef0);
    }
    
    public void setSensorCoefProp(int nsensor, double coefprop){
        conf.setProperty(getNameCoefPropSensor(nsensor), ""+coefprop);
    }
    
    public void saveAllCoefs(){
        conf.saveConfig();
    }
    
    public int[] getSensorCoef0() {
        int scoef0[] = new int[Configurator.COUNT_SENSOR];
        for (int i = 0; i < scoef0.length; i++) {
            scoef0[i] = getCoef0(getNameCoef0Sensor(i));
        }
        return scoef0;
    }

    public double[] getSensorCoefProp() {
        double scoefprop[] = new double[Configurator.COUNT_SENSOR];
        for (int i = 0; i < scoefprop.length; i++) {
            scoefprop[i] = getCoefProp(getNameCoefPropSensor(i));
        }
        return scoefprop;
    }
    
    public int getPositionCoef0(int nmotor) {
        return getCoef0(getNameCoef0Position(nmotor));
    }

    public double getPositionCoefProp(int nmotor) {
        return getCoefProp(getNameCoefPropPosition(nmotor));
    }
    
    public long getMaxPosition(int nmotor){
        return getMaxMinPos(getNameMaxPos(nmotor));
    }
    
    public long getMinPosition(int nmotor){
        return getMaxMinPos(getNameMinPos(nmotor));
    }
    
    
    public static CoefConfigurator getConf(){
        if(coef_conf==null){
            coef_conf = new CoefConfigurator();
        }
        return coef_conf;
    }
    
    public static double getNewCoefProp(long srcBeg, long srcEnd, double beg, double end){
        return (end-beg) / (srcEnd - srcBeg);
    }
    
    public static int getNewCoef0(long srcBeg, long srcEnd, double beg, double end){
        return getNewCoef0(srcBeg, beg, getNewCoefProp(srcBeg, srcEnd, beg, end));
    }
    
    public static int getNewCoef0(long srcx, double x, double propCoef){
        return (int) Math.round(srcx - (x/propCoef));
    }
    
}
